#!/bin/csh -f
set echo_style=both

set mode=2 # export full list of symmetry-dependent hkls so they can be checked
set mirror=1 # this is overriden by DATA.def in diffraction dir, but may be overriden again from the cmdline. mirror==0 sets LaueGroup=10/m, mirror==1 sets LaueGroup=10/m mm
set dpath="$DECAHOME/DiffData"
set dname="anc.t2s"  # default dataname
set tiling="tiling.inp"  # default tiling
set test=0
set mirrorset=0
set PHELP="awk -f $DECAHOME/SRC/pophelp.awk $DECAHOME/SRC/data2app.help"
set LSOPT="$DECAHOME/scripts/lsopt data2app"

# have to separate path from dataname, to check transformation matrix
while( "$*" != "" )
    if ( -e $1 ) then # config name is not option
	set tiling=$1
	shift
    endif
    if ( $# == 0 ) then
	goto skip_options
    endif
    switch ( $1 )
	case -data:
	    shift
	    set dname=$1
	breaksw
	case -path:
	    shift
	    set dpath=$1
	breaksw
	case -mode:
	    shift
	    set mode=$1
	breaksw
	case -mirror:
	    shift
	    set mirror=$1
	    set mirrorset=1
	breaksw
	case -test:
	    set test=1
	breaksw
	case -h:
	    goto helpmessage
	breaksw
        case -help:
	    helpmessage:
	    echo "Synopsis: data2app [tiling_name] [OPTIONS]"
	    echo "Default tiling_name is tiling.inp"
            shift
            if ( $# == 0 ) then
		$LSOPT
		exit
            endif
	    $PHELP $1
            exit
        breaksw
	default:
	    $LSOPT
	    exit
	breaksw
    endsw
    shift
end

skip_options:

#echo $dname > /dev/stderr
#exit

set DATA=$dpath/$dname
#echo "data2app: "$DATA
#echo "data2app: "$tiling

set datadat=`awk 'BEGIN{dn=ARGV[2];delete ARGV[2];ARGC--}$1==dn{print $2,$3}' $dpath/DATA.def $dname`
#echo $matfile
#exit
if ( $#datadat < 2 ) then
    echo "Data definition file DATA.def in the diffract directory doesnt give 2 values for $dname." > /dev/stderr
    exit
endif
set matfile=$datadat[1]
if ( $mirrorset == 0 ) set mirror=$datadat[2]
if (! -e $dpath/$matfile) then 
    echo "Missing data-transformation matrix file $dpath/$matfile"
    exit
endif
if (! -e INPUT/DFIT )then
    echo "Input directory INPUT/DFIT must exist." 
    exit
endif
if (! -e INPUT/PPOT/uc.inp ) then
    echo "File INPUT/PPOT/uc.inp is required."
    exit
endif
head -5 $dpath/$matfile > INPUT/DFIT/mat.hkl
if( -e $tiling && -e $DATA) then
    if($test == 0)then
	echo "TILING: "$tiling
	echo "$dname"
	echo "$mode\n$mirror\n$DATA\n$tiling" | $DECAHOME/bin/data2app_
    else
	echo "TILING: "$tiling > _dat.hkl_tmp
	echo "$mode\n$mirror\n$DATA\n$tiling" | $DECAHOME/bin/data2app_ >> _dat.hkl_tmp
	echo "###### MULTIPLY MAPPED DATAPOINTS" > test_hkl.dat
	echo "# Tiling: $tiling" >> test_hkl.dat
	echo "# Data: $dname" >> test_hkl.dat
	echo "Tiling: $tiling"
	echo "Data: $dname"
	awk -f $DECAHOME/SRC/test_hkl.awk _dat.hkl_tmp >> test_hkl.dat
	rm _dat.hkl_tmp
    endif
else
    if !( -e $tiling ) then
	echo "File $TILING does not exist."
    endif
    if !( -e $DATA ) then
	echo "File $DATA does not exist."
    endif
    if !( -e INPUT/PPOT/uc.inp ) then
	echo "File INPUT/PPOT/uc.inp does not exist."
    endif
    goto message
endif
exit
message:
echo "USAGE: data2app [-data DATANAME] [-path DATAPATH] [-mode MODE]"
echo "Maps decagonal diffraction data onto approximant data"
echo "Note: INPUT/PPOT/uc.inp must exist"
##

